//
//  HMComposeToolbar.swift
//  Weibo
//
//  Created by itteacher on 2017/7/31.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit

//  toolbar按钮类型
enum HMComposeToolbarButtonType: Int {
    //  图片
    case picture = 0
    //  @
    case mention = 1
    //  话题
    case trend = 2
    //  表情
    case emoticon = 3
    //  加号
    case add = 4
}

//  发微博工具条
class HMComposeToolbar: UIStackView {
    
    //  执行点击按钮的闭包
    var toolBarButtonClosure: ((HMComposeToolbarButtonType)->())?
    //  表情按钮
    var emoticonButton: UIButton!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加子控件 
    private func setupUI() {
        //  默认水平方向显示
        self.axis = .horizontal
        //  设置子视图的填充方式
        self.distribution = .fillEqually
        //  _ = 解决返回值不使用的方式1
        addChildButton(imageName: "compose_toolbar_picture", type: .picture)
        addChildButton(imageName: "compose_mentionbutton_background", type: .mention)
        addChildButton(imageName: "compose_trendbutton_background", type: .trend)
        emoticonButton = addChildButton(imageName: "compose_emoticonbutton_background", type: .emoticon)
        addChildButton(imageName: "compose_add_background", type: .add)
    }
    
    //  创建及添加子按钮
    @discardableResult //   忽略返回值，调用的时候不使用不会有警告（解决返回值不使用的方式2），swift 3.0如果返回值不使用会有警告
    private func addChildButton(imageName: String, type: HMComposeToolbarButtonType) -> UIButton {
        let button = UIButton()
        //  把枚举的原始值作为tag使用， 目的根据tag获取按钮对应枚举
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(buttonAction(btn:)), for: .touchUpInside)
        //  设置图片
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), for: .highlighted)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        //  取消高亮效果
        button.adjustsImageWhenHighlighted = false
        //  添加按钮
        addArrangedSubview(button)
        return button
    }
    
    //  MARK: --    点击toolbar按钮逻辑处理
    @objc private func buttonAction(btn: UIButton) {
        //  通过枚举的原始值 获取枚举
        let type = HMComposeToolbarButtonType(rawValue: btn.tag)!
        //  执行闭包完成回调
        toolBarButtonClosure?(type)
    
    }
    
    //  根据是否是表情键盘设置icon图片
    func switchButtonIcon(isEmoticon: Bool) {
        if isEmoticon {
            //  表情键盘，设置系统键盘的icon
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
        } else {
            //  系统键盘，设置表情键盘的icon
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
        }
    }
    
    
    
}
